﻿using System;
using System.Collections.Generic;
using System.Linq;
using Alex.Library.Specifications;

namespace Alex.Library.Tasks
{
    /// <summary>
    /// 任务管理器
    /// </summary>
    internal static class TaskManager
    {
        private static IDictionary<string, ITask> _TaskStore;

        #region Ctor

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static TaskManager()
        {
            _TaskStore = new Dictionary<string, ITask>();
        }

        #endregion

        #region Functions

        /// <summary>
        /// 注册任务
        /// </summary>
        /// <param name="task">任务</param>
        public static void RegisterTask(ITask task)
        {
            lock (_TaskStore)
            {
                _TaskStore[task.ID] = task;
            }
        }

        /// <summary>
        /// 移除已完成任务
        /// </summary>
        /// <param name="includeErrorTasks">是否包含发生异常的任务</param>
        public static void RemoveCompletedTask(bool includeErrorTasks)
        {
            lock (_TaskStore)
            {
                List<ITask> tasks;
                if (includeErrorTasks)
                {
                    tasks = _TaskStore.Values.Where(t => t.State == TaskState.Completed).ToList();
                }
                else
                {
                    tasks = _TaskStore.Values.Where(t => t.State == TaskState.Completed && !t.HasError).ToList();
                }
                if (tasks != null)
                {
                    foreach (ITask task in tasks)
                    {
                        _TaskStore.Remove(task.ID);
                    }
                }
            }
        }

        /// <summary>
        /// 查询任务
        /// </summary>
        /// <param name="id">任务标识</param>
        /// <returns>任务</returns>
        public static ITask FindTask(string id)
        {
            lock (_TaskStore)
            {
                return _TaskStore[id];
            }
        }

        /// <summary>
        /// 查询所有任务
        /// </summary>
        /// <returns>任务列表</returns>
        public static IList<ITask> FindAllTask()
        {
            lock (_TaskStore)
            {
                return _TaskStore.Values.ToList();
            }
        }

        /// <summary>
        /// 根据条件查询任务
        /// </summary>
        /// <param name="specification">查询条件</param>
        /// <returns>任务列表</returns>
        public static IList<ITask> FindTasks(ISpecification<ITask> specification)
        {
            lock (_TaskStore)
            {
                return _TaskStore.Values.Where(task => specification.IsSatisfiedBy(task)).ToList();
            }
        }

        /// <summary>
        /// 查询正在执行的任务
        /// </summary>
        /// <returns>正在执行的任务列表</returns>
        public static IList<ITask> FindDoingTasks()
        {
            return FindTasks(Specification<ITask>.Eval(task => task.State == TaskState.Doing));
        }

        /// <summary>
        /// 查询发生异常的任务
        /// </summary>
        /// <returns>异常任务列表</returns>
        public static IList<ITask> FindErrorTasks()
        {
            return FindTasks(Specification<ITask>.Eval(task => task.HasError));
        }

        /// <summary>
        /// 查询已完成的任务
        /// </summary>
        /// <returns>已完成任务列表</returns>
        public static IList<ITask> FindCompletedTasks()
        {
            return FindTasks(Specification<ITask>.Eval(task => task.State == TaskState.Completed));
        }

        #endregion
    }
}
